/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 02.01.2006
 *
 *************************************************/
package fightplan.persistence;

import static fightplan.persistence.ImportHelper.getBoolean;
import static fightplan.persistence.ImportHelper.getString;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import fightplan.business.Category;
import fightplan.business.Dojo;
import fightplan.business.Fighter;
import fightplan.business.Gender;
import fightplan.business.Team;
import fightplan.business.Tournament;

public class ExcelSkrImport2010 {

	static void imprtFightersAndTeams(Tournament tournament, File file) throws Exception {
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
		HSSFWorkbook wb = new HSSFWorkbook(fs);

		HSSFSheet sheet = wb.getSheetAt(0);

		// ////////////////////////////////////////////////
		// if (!checkSheet(sheet, (short)0, (short)36, "Format 080315")) throw
		// new
		// IllegalArgumentException("Kein gültiges Excelsheet: " +
		// file.getName());

		// Dojo
		String dojoName = getString(sheet.getRow(3), 2);
		Dojo dojo = tournament.getOrCreateDojo(dojoName);

		// read Fighters
		readFighters(tournament, dojo, sheet);

		// read Teams
		readTeams(tournament, dojo, sheet);
	}

	private static void readFighters(Tournament tournament, Dojo dojo, HSSFSheet sheet) {
		for (int i = 18; i < 169; i++) {
			HSSFRow row = sheet.getRow(i);
			if (row == null)
				continue;
			String fighterName = getString(row, 1);
			if (fighterName == null || fighterName.length() == 0)
				continue;

			fighterName = ImportHelper.normalizeName(fighterName);

			Fighter fighter = new Fighter();
			fighter.setName(fighterName);
			fighter.setDojo(dojo);

			int c = 4; // column

			try {
				fighter.setYearFormatted(getString(row, c++));
			} catch (IllegalArgumentException iae) {
				// silent
			}

			String kyuString = getString(row, c++);
			if (kyuString != null && kyuString.length() > 0 && Character.isDigit(kyuString.charAt(0))) {
				fighter.setBelt(-Integer.parseInt(kyuString.substring(0, 1)));
			}

			String weightString = getString(row, c++);
			if (weightString != null) {
				try {
					fighter.setWeightFormatted(weightString);
				} catch (IllegalArgumentException iae) {
					// silent
				}
			}

			//

			readKataColumn(tournament, fighter, row, c++, true, null);
			readKataColumn(tournament, fighter, row, c++, true, Gender.D);
			readKataColumn(tournament, fighter, row, c++, true, Gender.H);
			readKataColumn(tournament, fighter, row, c++, true, Gender.D);
			readKataColumn(tournament, fighter, row, c++, true, Gender.H);

			readIpponColumn(tournament, fighter, row, c++, 17, 25, null); // 12
			readIpponColumn(tournament, fighter, row, c++, 26, 33, Gender.D); // 13
			readIpponColumn(tournament, fighter, row, c++, 26, 33, Gender.H);
			readIpponColumn(tournament, fighter, row, c++, 34, 45, Gender.D); // 15
			readIpponColumn(tournament, fighter, row, c++, 34, 45, Gender.H);

			tournament.getFighters().add(fighter);
		}
	}

	private static void readKataColumn(Tournament tournament, Fighter fighter, HSSFRow row, int column, boolean kata,
			Gender gender) {
		if (getBoolean(row, column)) {
			fighter.setGender(gender);
			Category category = tournament.findCategory(fighter, kata, false); // Kata,
																				// elite
																				// argument
																				// is
																				// ignored
			if (category != null) {
				category.getFighters().add(fighter);
			} else {
				System.out.println("Probleme mit Kata: " + fighter.getName());
			}
		}
	}

	private static void readIpponColumn(Tournament tournament, Fighter fighter, HSSFRow row, int column,
			int minCategory, int maxCategory, Gender gender) {
		if (getBoolean(row, column)) {
			if (gender != null) {
				fighter.setGender(gender);
			}
			Category category = tournament.findKumiteCategory(fighter, minCategory, maxCategory);
			if (category != null) {
				category.getFighters().add(fighter);
			} else {
				System.out.println("Probleme mit: " + fighter.getName() + " column: " + column);
			}
		}
	}

	private static void readTeams(Tournament tournament, Dojo dojo, HSSFSheet sheet) {
		readTeams(tournament, dojo, sheet, 4, 10, 46, 6); // kata
		readTeams(tournament, dojo, sheet, 5, 15, 52, 2); // kumite
	}

	private static void readTeams(Tournament tournament, Dojo dojo, HSSFSheet sheet, int row, int column,
			int baseCategory, int count) {

		for (int i = 0; i < count; i++) {
			HSSFRow hssfRow = sheet.getRow(row + i);
			String anzahlString = getString(hssfRow, column);
			if (anzahlString == null || anzahlString.length() == 0)
				continue;

			int anzahl = Integer.parseInt(anzahlString);
			if (anzahl == 0)
				continue;

			Category category = null;
			for (Category c : tournament.getCategories()) {
				if (c.getCategoryId() == baseCategory + i) {
					category = c;
					break;
				}
			}
			addTeams(category, dojo, anzahl);
		}
	}

	private static void addTeams(Category category, Dojo dojo, int anzahl) {
		if (anzahl == 1) {
			Team team = new Team();
			team.setDojo(dojo);
			team.setCategory(category);
			category.addFighter(team);
		} else {
			for (int j = 0; j < anzahl; j++) {
				Team team = new Team();
				team.setDojo(dojo);
				team.setCategory(category);
				team.setNumber(j + 1);
				category.addFighter(team);
			}
		}
	}

}
